package com.tinyengine.it.controller.endpoint;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.common.utils.ControllerParamsJavaDataType;
import com.tinyengine.it.model.vo.PredefinedRequest;
import com.tinyengine.it.service.endpoint.CommonService;
import com.tinyengine.it.tool.api.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/return/structure/api")
@Tag(name = "返回参数结构")
@RequiredArgsConstructor
public class ReturnStructureController {

    private final CommonService commonService;

    @Operation(summary = "预定义响应期望，根据逻辑实体Id",
            parameters = {
                    @Parameter(name = "voId", description = "ID"),
                    @Parameter(name = "dataType", description = "数据类型")
            },
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json")),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @PostMapping(value = "/predefinedResponseExpectation")
    public Result predefinedResponseExpectation(@RequestBody PredefinedRequest request) {

        ControllerParamsJavaDataType javaDataType = ControllerParamsJavaDataType.fromValue(request.getDataType());

        switch (javaDataType) {
            case String:
                return Result.success(R.data(""));

            case Object:
                MapBuilder<String, Object> mapBuilderObj = MapUtil.builder();
                commonService.processFields(request.getVoId(), mapBuilderObj);
                return Result.success(R.data(mapBuilderObj.build()));

            case List_Object:
                MapBuilder<String, Object> mapBuilderList = MapUtil.builder();
                commonService.processFields(request.getVoId(), mapBuilderList);
                return Result.success(R.data(Arrays.asList(mapBuilderList.build())));

            case Page_Object:
                Page<Object> stringPage = new Page<>();
                MapBuilder<String, Object> mapBuilderPage = MapUtil.builder();
                commonService.processFields(request.getVoId(), mapBuilderPage);
                stringPage.setRecords(Arrays.asList(mapBuilderPage.build()));
                return Result.success(R.data(stringPage));
        }

        return Result.success(Result.success());

    }
}

